package com.chencong.online.function;

import com.chencong.online.bean.LogFailWarningBean;
import com.chencong.online.bean.LoginEventBean;
import com.chencong.online.utils.TimestampTransformUtil;
import org.apache.flink.cep.PatternSelectFunction;

import java.util.List;
import java.util.Map;

/**
 * @program: user-behavior-analysis-online
 * @ClassName LoginFailPattenFunc
 * @description:实现自定义的PatternSelectFunction
 * @author: chencong
 * @create: 2021-12-31 11:13
 **/
public class LoginFailPattenFunc implements PatternSelectFunction<LoginEventBean, LogFailWarningBean> {
    @Override
    public LogFailWarningBean select(Map<String, List<LoginEventBean>> pattern) throws Exception {
        LoginEventBean firstFail = pattern.get("firstFail").get(0);
        LoginEventBean secondFail = pattern.get("secondFail").get(0);
        return new LogFailWarningBean(firstFail.getUserId(), TimestampTransformUtil.milliTimestampToLocalDateTime(firstFail.getTimestamp() * 1000L)
                , TimestampTransformUtil.milliTimestampToLocalDateTime(secondFail.getTimestamp() * 1000L), "login fail 2 times");
    }
}
